#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

// 定义 Video 结构体（类似 C 的结构体）
struct Video {
	string title;
	string author;
	string link;
};

// 全局变量：存储所有视频
vector<Video> videos;

// 文件名
const string FILENAME = "videos.txt";

// 从文件加载视频数据
void loadVideos() {
	ifstream file(FILENAME);
	if (!file.is_open()) {
		return; // 文件不存在，直接返回
	}
	
	Video v;
	while (getline(file, v.title, ',')) {  // 按 ',' 分隔读取
		getline(file, v.author, ',');
		getline(file, v.link);
		videos.push_back(v);
	}
	file.close();
}

// 保存视频数据到文件
void saveVideos() {
	ofstream file(FILENAME);
	if (!file.is_open()) {
		cout << "无法保存文件！" << endl;
		return;
	}
	
	for (const auto& v : videos) {
		file << v.title << "," << v.author << "," << v.link << "\n";
	}
	file.close();
}

// 添加视频
void addVideo() {
	Video v;
	cout << "请输入视频标题: ";
	cin.ignore();  // 清除缓冲区
	getline(cin, v.title);
	
	cout << "请输入视频作者: ";
	getline(cin, v.author);
	
	cout << "请输入视频链接: ";
	getline(cin, v.link);
	
	videos.push_back(v);
	saveVideos();
	cout << " 视频已添加到收藏夹！" << endl;
}

// 查看所有视频
void viewVideos() {
	if (videos.empty()) {
		cout << " 收藏夹为空！" << endl;
		return;
	}
	
	cout << "\n 收藏夹中的视频：" << endl;
	for (size_t i = 0; i < videos.size(); i++) {
		cout << i + 1 << ". "
		<< "标题: " << videos[i].title << "\t"
		<< "作者: " << videos[i].author << "\t"
		<< "链接: " << videos[i].link << endl;
	}
}

// 删除视频
void deleteVideo() {
	if (videos.empty()) {
		cout << " 收藏夹为空，无法删除！" << endl;
		return;
	}
	
	viewVideos();  // 先显示所有视频
	int index;
	cout << "请输入要删除的视频编号: ";
	cin >> index;
	
	if (index < 1 || index > videos.size()) {
		cout << " 无效的编号！" << endl;
		return;
	}
	
	videos.erase(videos.begin() + index - 1);  // 删除指定视频
	saveVideos();
	cout << " 视频已删除！" << endl;
}

// 主菜单
void showMenu() {
	cout << "\n 抖音收藏夹管理系统 " << endl;
	cout << "1. 添加视频" << endl;
	cout << "2. 查看所有视频" << endl;
	cout << "3. 删除视频" << endl;
	cout << "0. 退出" << endl;
	cout << "请选择操作: ";
}

int main() {
	loadVideos();  // 启动时加载数据
	
	int choice;
	do {
		showMenu();
		cin >> choice;
		
		switch (choice) {
		case 1:
			addVideo();
			break;
		case 2:
			viewVideos();
			break;
		case 3:
			deleteVideo();
			break;
		case 0:
			cout << "再见！" << endl;
			break;
		default:
			cout << " 无效选择，请重新输入！" << endl;
		}
	} while (choice != 0);
	
	return 0;
}